#include "CharaManager.h"
#include "PlayerChara.h"
#include "EnemyChara.h"
#include "../../Map/Map.h"
#include "../../Map/MapActionManager.h"
#include "../../Common/Common.h"

// -------------------------------- //

CharaManager::CharaManager(void)
	: Singleton( this )
{
}

// -------------------------------- //

CharaManager::~CharaManager(void)
{
	Clear();
}

// -------------------------------- //

void CharaManager::Clear()
{
	int size = m_characters.size();
	for ( int i = 0; i < size; ++i )
	{
		delete m_characters[i];
		m_characters[i] = NULL;
	}

	m_characters.clear();
}

// -------------------------------- //

Character* CharaManager::GetCharacter( int index )
{
	if ( index < 0 
		|| index >= GetCount() )
	{
		return NULL;
	}

	return m_characters[ index ];
}

// -------------------------------- //

void CharaManager::Update( float delta )
{
	int size = m_characters.size();
	for ( int i = 0; i < size; ++i )
	{
		m_characters[i]->Update( delta );
	}

	for ( int i = 0; i < m_characters.size(); ++i )
	{
		if ( m_characters[i]->IsDead() )
		{
			Character* pChara = m_characters[i];
			m_characters.erase( m_characters.begin() + i );
			delete pChara;
			i--;
		}
	}
}

// -------------------------------- //

void CharaManager::Draw()
{
	int size = m_characters.size();
	for ( int i = 0; i < size; ++i )
	{
		m_characters[i]->Draw();
	}
}

// -------------------------------- //

PlayerChara* CharaManager::CraetePlayerChara( GameDefine::PLAYER_INDEX id, const Character::Parameter& param, 
											  const Point& mapPoint, const Vector2& pos, int textureHandle )
{
	PlayerChara* pChara = new PlayerChara( id, param, mapPoint, pos, textureHandle );
	m_characters.push_back( pChara );
	MapActionManager::GetInstance().SetPlayer( pChara );
	return pChara;
}

// -------------------------------- //

EnemyChara* CharaManager::CreateEnemyChara( unsigned int type, const Character::Parameter& param, 
											const Point& mapPoint, const Vector2& pos, int textureHandle )
{
	EnemyChara::TYPES enemyType = static_cast<EnemyChara::TYPES>( type );
	EnemyChara* pChara = new EnemyChara( enemyType, param, mapPoint, pos, textureHandle );
	m_characters.push_back( pChara );
	return pChara;
}


